package com.example.demo;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String keyword){
        return "接收参数："+keyword;
    }


    @RequestMapping("/r2")
    public String r2(String useName ,String passWord){
        return "接收参数：useName:"+ useName +",passWord:" +passWord;
    }


    @RequestMapping("/r3")
    public String r3(Integer keyword){
        return "接收参数："+keyword;
    }


    @RequestMapping("/r4")
    public String r4(int keyword){
        return "接收参数："+keyword;
    }

    @RequestMapping("/r5")
    public String r5(UserInfo userInfo){
        return "接收参数: userInfo="+userInfo.toString();
    }

    //从前端接收参数q, 赋值给keyword
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "q", required = false) String keyword){
        return "接收参数: keyword="+keyword;
    }

    //传递数组
    @RequestMapping("/r7")
    public String r7(String[] arr){
        return "接收参数: arr="+ Arrays.toString(arr);
    }

    //传递集合
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list){
        return "接收参数: list="+ list;
    }

    //传递json
    @RequestMapping("/r9")
    public String r9(@RequestBody UserInfo userInfo){
        //System.out.println(userInfo.getName());
        return userInfo.toString();
    }

    //从ur1中获取参数
    @RequestMapping("/article/{articleId}")
    public String r10(@PathVariable Integer articleId){
        return "获取文章ID:" + articleId ;
    }

    @RequestMapping("/article/{articleId}/{type}")
    public String r11(@PathVariable Integer articleId, @PathVariable("type") String articletype){
        return "获取文章ID: "+ articleId + ",type: " + articletype;
    }


    //上传文件
    @RequestMapping("/r12")
    public String r12(@RequestPart("file11")MultipartFile file)throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("D:\\temp\\" +file.getOriginalFilename()));
        return "文件上传成功";
    }

}
